package com.ruoyi.electronicFence.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.electronicFence.domain.MonitoringParameters;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.electronicFence.domain.TDeviceStatus;
import com.ruoyi.electronicFence.service.ITDeviceStatusService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 监测设备
 *
 * @author sxygsj
 * @date 2024-05-17
 */
@RestController
@RequestMapping("/electronicFence/status")
public class TDeviceStatusController extends BaseController
{
    @Autowired
    private ITDeviceStatusService tDeviceStatusService;

    /**
     * 查询设备状态列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDeviceStatus tDeviceStatus)
    {
        startPage();
        List<TDeviceStatus> list = tDeviceStatusService.selectTDeviceStatusList(tDeviceStatus);
        return getDataTable(list);
    }

    @GetMapping("/history-list")
    public TableDataInfo historyList(TDeviceStatus tDeviceStatus)
    {
        LocalDateTime statusStartTime = tDeviceStatus.getStatusStartTime();
        LocalDateTime statusEndTime = tDeviceStatus.getStatusEndTime();

        startPage();
        List<TDeviceStatus> list = tDeviceStatusService.selectTDeviceStatusHistoryList(tDeviceStatus);
        return getDataTable(list);
    }

        //
    /**
     * 导出设备状态列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:export')")
    @Log(title = "设备状态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDeviceStatus tDeviceStatus)
    {
        List<TDeviceStatus> list = tDeviceStatusService.selectTDeviceStatusList(tDeviceStatus);
        ExcelUtil<TDeviceStatus> util = new ExcelUtil<TDeviceStatus>(TDeviceStatus.class);
        util.exportExcel(response, list, "设备状态数据");
    }

    /**
     * 获取设备状态详细信息
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tDeviceStatusService.selectTDeviceStatusById(id));
    }

    /**
     * 新增设备状态
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:add')")
    @Log(title = "设备状态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDeviceStatus tDeviceStatus)
    {
        return tDeviceStatusService.insertTDeviceStatus(tDeviceStatus);
    }

    /**
     * 修改设备状态
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:edit')")
    @Log(title = "设备状态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDeviceStatus tDeviceStatus)
    {
        return tDeviceStatusService.updateTDeviceStatus(tDeviceStatus);
    }



    /**
     * 修改设备状态
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:edit')")
    @Log(title = "设备状态", businessType = BusinessType.UPDATE)
    @PutMapping("/synchronous/update")
    public AjaxResult synchronousUpdate(@RequestBody TDeviceStatus tDeviceStatus)
    {
        return toAjax(tDeviceStatusService.synchronousUpdate(tDeviceStatus));
    }

    /**
     * 删除设备状态
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:remove')")
    @Log(title = "设备状态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tDeviceStatusService.deleteTDeviceStatusByIds(ids));
    }

    /**
     * 删除设备状态V2
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:remove')")
    @Log(title = "设备状态", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestBody TDeviceStatus tDeviceStatus)
    {
        return toAjax(tDeviceStatusService.deleteTEquipmentMonitorDeviceByMonitorDeviceId(tDeviceStatus));
    }

    /**
     * 查询设备状态列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:list')")
    @GetMapping("/settings/list")
    public AjaxResult selectSettingsList(String Type,String IP)
    {
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> map = tDeviceStatusService.selectSettingsList(Type, IP);
        if (map.get("code")!=null){
            return new AjaxResult(500,"设备离线，无法查看详情");
        }
        ajaxResult.put("data",map);
        return ajaxResult;
    }

    /**
     * 修改设备状态列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:list')")
    @PostMapping("/settings/update")
    public AjaxResult updateSettingsList(@RequestBody  Map<String, Object> monitoringParameters)
    {
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> map = tDeviceStatusService.updateSettingsList(monitoringParameters);
        ajaxResult.put("data",map);
        //记录修改历史
        try {
            tDeviceStatusService.recordUpdateHistory(monitoringParameters);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }


    /**
     * 查询设备状态列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:list')")
    @PostMapping("/settings/maintenanceMode")
    public AjaxResult selectMaintenanceMode(@RequestBody TDeviceStatus tDeviceStatus)
    {
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> map = tDeviceStatusService.selectMaintenanceMode(tDeviceStatus);
        if (map.get("code")!=null){
            return new AjaxResult(500,"设备离线，无法查看详情");
        }
        ajaxResult.put("data",map);
        return ajaxResult;
    }

    /**
     * 修改设备状态列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:status:list')")
    @PostMapping("/settings/maintenanceMode/update")
    public AjaxResult updateMaintenanceMode(@RequestBody Map<String, Object> monitoringParameters)
    {
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> map = tDeviceStatusService.updateMaintenanceMode(monitoringParameters);
        ajaxResult.put("data",map);
        //记录修改历史
        try {
            tDeviceStatusService.recordMaintenanceModeUpdateHistory(monitoringParameters);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
